﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TGVR.Manager.Pages.Template;
using Telerik.Web.UI;
using MDF.DataHelper.BussinessObject.UsersBO;
using MDF.DataHelper.BussinessLogic.UsersBL;
using WebLibs;

namespace TGVR.Manager.Pages.Account
{
    public partial class Account_List : TGVRPageBase
    {
        private int iPageSize = 10;

        public int PageSize
        {
            get { return iPageSize; }
            set { iPageSize = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.User == null)
                HttpContext.Current.Response.Redirect(Globals.ApplicationVRoot() + "/Login.aspx", true);
            if (!IsPostBack)
            {
                this.InitData();
            }
        }

        private void InitData()
        {
            //Load UserGroup
            var lstUserGroup = UserGroupBUS.Current.GetAll();
            ddlUserGroup.DataSource = lstUserGroup;
            ddlUserGroup.DataBind();
            ddlUserGroup.Items.Insert(0,new ListItem("Tất cả","0"));
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            grvData.CurrentPageIndex = 0;
            grvData.Rebind();
        }

        protected void grvData_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            List<UserProfile> lstAccount = null;
            if (User.IsInRoles(Permission.UserPermissions.CanViewAccountsFull))
            {
                lstAccount = UserProfileBUS.Current.GetAll(txtSearch.Text,Int32.Parse( ddlUserGroup.SelectedValue), 0, grvData.CurrentPageIndex + 1, PageSize);
            }
            else
            {
                if (User.IsInRoles(Permission.UserPermissions.CanViewAccounts))
                {
                    string lstUserID = "";
                    foreach (int item in User.Childs)
                    {
                        lstUserID += item + ",";
                    }
                    if (lstUserID.Length > 0)
                        lstUserID = lstUserID.Substring(0, lstUserID.Length - 1);
                    lstAccount = UserProfileBUS.Current.GetList(txtSearch.Text, lstUserID, grvData.CurrentPageIndex + 1, PageSize);
                }
            }
            if (lstAccount.Count > 0)
            {
                grvData.DataSource = lstAccount;
                grvData.MasterTableView.VirtualItemCount = lstAccount[0].Total;
                grvData.CurrentPageIndex = grvData.MasterTableView.CurrentPageIndex;
            }
            else
            {
                grvData.DataSource = lstAccount;
                grvData.MasterTableView.VirtualItemCount = 0;
            }
        }

        protected void grvData_ItemCommand(object sender, GridCommandEventArgs e)
        {
            string ID = e.CommandArgument.ToString();

            switch (e.CommandName)
            {
                case "DeleteRow":
                    try
                    {
                        UserProfileBUS.Current.Delete(Int32.Parse(ID), User.UserId);
                        grvData.Rebind();
                        MsgBox.ShowInfo("Xóa thành viên thành công");
                    }
                    catch (Exception ex)
                    {
                        MsgBox.ShowError("Lỗi xóa thành viên");
                    }
                    break;
                case "Active":
                    try
                    {
                        UserMembershipBUS.Current.UpdateActive(Int32.Parse(ID), true, User.UserId);
                        grvData.Rebind();
                        MsgBox.ShowInfo("Kích hoạt thành viên thành công");
                    }
                    catch (Exception ex)
                    {
                        MsgBox.ShowError("Lỗi kích hoạt thành viên");
                    }
                    break;
                case "DeActive":
                    try
                    {
                        UserMembershipBUS.Current.UpdateActive(Int32.Parse(ID), false, User.UserId);
                        grvData.Rebind();
                        MsgBox.ShowInfo("Bỏ kích hoạt thành viên thành công");
                    }
                    catch (Exception ex)
                    {
                        MsgBox.ShowError("Lỗi bỏ kích hoạt thành viên");
                    }
                    break;
            }
        }
    }
}